package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.user.impl.dao.BreedFactoryStableDao;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.BreedFactoryStable;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.ProductionReportDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.ProductionReport;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by maowenjie on 2017/4/6.
 */
@Component
public class ProductionReportManager {
    private final ProductionReportDao productionReportDao;
    private final ReportHandleResumeDao reportHandleResumeDao;
    private final BreedFactoryDao breedFactoryDao;
    private final BreedFactoryStableDao breedFactoryStableDao;

    @Autowired
    public ProductionReportManager(ProductionReportDao productionReportDao,
                                   ReportHandleResumeDao reportHandleResumeDao,
                                   BreedFactoryDao breedFactoryDao,
                                   BreedFactoryStableDao breedFactoryStableDao) {
        this.productionReportDao = productionReportDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
        this.breedFactoryDao = breedFactoryDao;
        this.breedFactoryStableDao = breedFactoryStableDao;
    }

    /**
     * 新建生产中心-生产记录信息表
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> createProductionReport(ProductionReport productionReport, Integer toStatus){
        Integer fromStatus = productionReport.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();
        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "SSC";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,productionReportDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        productionReport.setNumber(number);
        productionReport.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            productionReport.setStatus(toStatus);
            productionReportDao.create(productionReport);
            map.put("productionReportId",productionReport.getId());

            //改变存栏数

            BreedFactory breedFactory = new BreedFactory();
            breedFactory.setId(productionReport.getBreedFactoryId());
            breedFactory.setStockQuantity(productionReport.getChangeQuantity());
            breedFactoryDao.updateFromSC(breedFactory);
            if(productionReport.getStableId() != null){
                BreedFactoryStable breedFactoryStable = new BreedFactoryStable();
                breedFactoryStable.setId(productionReport.getStableId());
                breedFactoryStable.setQuantity(productionReport.getChangeQuantity());
                breedFactoryStableDao.updateFromSC(breedFactoryStable);
            }

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(productionReport.getId());
            reportHandleResume.setReportNumber(productionReport.getNumber());
            reportHandleResume.setType(DocumentsType.PRODUCTION_REPORT.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            productionReportDao.create(productionReport);
            map.put("productionReportId",productionReport.getId());
        }
        return map;
    }
    /**
     * 更新生产中心-生产记录信息表
     * @param productionReport 生产中心-生产记录信息表
     * @param toStatus 到状态
     * @return Map<String , Object>
     */
    @Transactional
    public Map<String , Object> updateProductionReport(ProductionReport productionReport,Integer toStatus){
        Integer fromStatus = productionReport.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagProductionReport;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            productionReport.setStatus(toStatus);
            flagProductionReport = productionReportDao.update(productionReport);

            map.put("flagProductionReport",flagProductionReport);

            //改变存栏数
            LoginUser loginUser = UserUtil.getCurrentUser();
            BreedFactory breedFactory = new BreedFactory();
            breedFactory.setId(productionReport.getBreedFactoryId());
            breedFactory.setStockQuantity(productionReport.getChangeQuantity());
            breedFactoryDao.updateFromSC(breedFactory);
            if(productionReport.getStableId() != null){
                BreedFactoryStable breedFactoryStable = new BreedFactoryStable();
                breedFactoryStable.setId(productionReport.getStableId());
                breedFactoryStable.setQuantity(productionReport.getChangeQuantity());
                breedFactoryStableDao.updateFromSC(breedFactoryStable);
            }

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(productionReport.getId());
            reportHandleResume.setReportNumber(productionReport.getNumber());
            reportHandleResume.setType(DocumentsType.PRODUCTION_REPORT.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagProductionReport = productionReportDao.update(productionReport);
            map.put("flagProductionReport",flagProductionReport);
        }
        return map;
    }
}
